package main;

import java.util.Vector;

public abstract class Neuron {
	protected Vector<Input> ins;
	protected Vector<Neuron> outs;


	private boolean netComputed;
	private double netValue;

	public Neuron() {
		netComputed = false;
		netValue = 0;
		ins = new Vector<Input>();
		outs = new Vector<Neuron>();
	}
	
	public void connectIn(Neuron n, double weight){
		ins.add(new Input(n, weight));
	}
	
	public void connectOut(Neuron n){
		outs.add(n);
	}
	
	public void reset(){
		netComputed = false;
		netValue = 0;
	}
	
	public double getNet(){
		if (!netComputed){
			netValue = 0;
			for(Input in: ins){
				netValue = netValue + in.getWeight()*in.getNeuron().getValue();
			}
			netComputed = true;
		}
		return netValue;
	}
	
	public abstract double getValue();
	
	public double getWeight(Neuron n) {
		for (Input in: ins){
			if (in.getNeuron() == n){
				return in.getWeight();
			}
		}
		return 0;
	}

	public static class Input {
		Neuron neuron;
		double weight;
		public Input(Neuron neuron, double weight){
			this.neuron = neuron;
			this.weight = weight;
		}

		public Neuron getNeuron() {
			return neuron;
		}
		
		public double getWeight() {
			return weight;
		}
		
		public void setWeight(double weight) {
			this.weight = weight;
		}
	}

	public abstract Neuron createInstance();
}
